package com.mall.shopping.services;

import com.mall.shopping.ITestProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.TestProductDetailConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dto.TestProductDetailDto;
import com.mall.shopping.dto.TestProductRequest;
import com.mall.shopping.dto.TestProductResponse;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class ITestProductServiceImpl implements ITestProductService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    TestProductDetailConverter productDetailConverter;

    @Override
    public TestProductResponse getProductDetail(TestProductRequest request) {

        TestProductResponse testProductResponse = new TestProductResponse();

        try {
            // 有效参数校验
            request.requestCheck();
            //在这里访问数据库有可能失败，我在者没有处理，异常就会转接给调用他的人，调用信息会给前端，就会把网站完整的信息，完全暴露给用户了
            /*业务层处了问题由业务层自己处理错误，用try catch捕获异常，那collener怎么知道，本次捕获异常是成功还是失败*/
            //数据库单表查询出来的DO对象，虽然由很多数据，但是在业务层只显示三个数据，等
            Item item = itemMapper.selectByPrimaryKey(request.getProductId());

            /*现在对do的属性对象做一个裁剪，做一个do对象对dto对象的一个转换，然后做一个converter，现在就是把item
            * 对象转换为TestProductDetailDto对象*/


//            String[] images = item.getImages();
//            item.setImageBig(images[0]);

            TestProductDetailDto testProductDetailDto
                    = productDetailConverter.testProductToProductDto(item);

            testProductResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            testProductResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            testProductResponse.setProductDetailDto(testProductDetailDto);
        } catch (Exception e) {
            testProductResponse.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            testProductResponse.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
        }

        //然后将服务返回
        return testProductResponse;
    }




}
